//
//  GoodsListViewController.m
//  WuJieLive
//
//  Created by haidong on 2017/12/25.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "GoodsListViewController.h"
#import "SearchActionView.h"
#import "GoodsSiftView.h"
#import "GoodsListColCell.h"
#import "GoodsListColReusableView.h"

typedef NS_ENUM(NSInteger, GoodsListType) {
    GoodsListTypeNormal,
    GoodsListTypeEmpty
};

#define kGoodsListItemSize          CGSizeMake(kScreenWidth/2.0 - 0.f, (kScreenWidth/2.0 - 0.f) + 120.f)
#define kGoodsListEmptyHeaderSize   CGSizeMake(kScreenWidth, 320.f)

@class SearchViewController;

@interface GoodsListViewController ()
<
UICollectionViewDelegateFlowLayout,
UICollectionViewDataSource,
BaseViewDelegate
>

@property (assign, nonatomic)GoodsListType goodsListType;

@property (strong, nonatomic)SearchActionView *searchView;
@property (strong, nonatomic)GoodsSiftView *goodsSiftView;
@property (strong, nonatomic)UICollectionView *goodsCollectionView;

@property (strong, nonatomic)NSMutableArray *goodsDataArray;
@property (strong, nonatomic)NSMutableDictionary *paramDic;
@property (assign, nonatomic)NSInteger page;

@end

@implementation GoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)setNavi {
    self.navigationItem.titleView = self.searchView;
}

- (void)setUI {
    
}

- (void)setData {
    self.page = 1;
    [self.paramDic setValue:[NSString stringWithFormat:@"%ld", self.page] forKey:@"page"];
    [self.paramDic setValue:@"" forKey:@"search"];
    if (self.keywordStr) {
        [self.paramDic setValue:self.keywordStr forKey:@"key_words"];
        [self.searchView bindSearchKeyword:self.keywordStr];
    }
    [self getGoodsList];
}

- (void)refreshGoodsList {
    if (self.page == 1) {
        [self.goodsDataArray removeAllObjects];
    }
    WS(weakSelf)
    [MBProgressHUD showMessage:nil toView:self.view];
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@index/search",BaseUrl] para:self.paramDic success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (isSuccess) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSDictionary class]]) {
                NSArray *goodsArr = [data arrForKey:self.goodsListType == GoodsListTypeNormal ? @"goods" : @"boutique"];
                [weakSelf.goodsDataArray addObjectsFromArray:[GoodsListEntity mj_objectArrayWithKeyValuesArray:goodsArr]];
                [weakSelf.goodsCollectionView reloadData];
            }
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
    }];
    
}

- (void)getGoodsList {
    WS(weakSelf)
    [MBProgressHUD showMessage:nil toView:self.view];
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@index/search",BaseUrl] para:self.paramDic success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        if (isSuccess) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSDictionary class]]) {
                NSArray *goods = [data arrForKey:@"goods"];
                NSArray *boutique = [data arrForKey:@"boutique"];
                if (goods.count > 0) {
                    weakSelf.goodsListType = GoodsListTypeNormal;
                    [weakSelf.goodsDataArray addObjectsFromArray:[GoodsListEntity mj_objectArrayWithKeyValuesArray:goods]];
                    [weakSelf loadNormalList];
                } else {
                    weakSelf.goodsListType = GoodsListTypeEmpty;
                    [weakSelf.goodsDataArray addObjectsFromArray:[GoodsListEntity mj_objectArrayWithKeyValuesArray:boutique]];
                    [weakSelf loadRecommendList];
                }
            }
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
    }];
}

- (void)loadNormalList {
    WS(weakSelf)
    [self.view addSubview:self.goodsSiftView];
    [self.goodsSiftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44.f);
    }];
    [self.view addSubview:self.goodsCollectionView];
    [self.goodsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.goodsSiftView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)loadRecommendList {
    [self.view addSubview:self.goodsCollectionView];
    [self.goodsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.goodsDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodsListColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodsListColCellID" forIndexPath:indexPath];
    [cell bindData:self.goodsDataArray[indexPath.row]];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader && self.goodsListType == GoodsListTypeEmpty) {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"GoodsListColReusableViewID" forIndexPath:indexPath];
    }
    return reusableView;
}

- (void)viewActionWithView:(UIView *)view tag:(NSInteger)tag data:(id)data {
    if ([view isKindOfClass:[SearchActionView class]]) {
        for (UIViewController *vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[SearchViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                return;
            }
        }
        SearchViewController *vc = [SearchViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

- (SearchActionView *)searchView {
    if (!_searchView) {
        _searchView = [[SearchActionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 100, 30)];
        _searchView.delegate = self;
    }
    return _searchView;
}

- (GoodsSiftView *)goodsSiftView {
    if (!_goodsSiftView) {
        _goodsSiftView = (GoodsSiftView *)[GoodsSiftView loadFromNib];
        WS(weakSelf)
        _goodsSiftView.selectBlock = ^(NSInteger tag, BOOL isDown) {
            switch (tag) {
                case 0:
                    [weakSelf.paramDic setValue:@"" forKey:@"search"];
                    break;
                case 1:
                    [weakSelf.paramDic setValue:@"sale" forKey:@"search"];
                    break;
                case 2:
                    [weakSelf.paramDic setValue:isDown ? @"down" : @"up" forKey:@"search"];
                    break;
                case 3:
                    
                    break;
                default:
                    break;
            }
            weakSelf.page = 1;
            [weakSelf refreshGoodsList];
        };
    }
    return _goodsSiftView;
}

- (UICollectionView *)goodsCollectionView {
    if (!_goodsCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 0.f;
        flowLayout.minimumInteritemSpacing = 0.f;
        flowLayout.itemSize = kGoodsListItemSize;
        if (self.goodsListType == GoodsListTypeEmpty) {
            flowLayout.headerReferenceSize = kGoodsListEmptyHeaderSize;
        }
        _goodsCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _goodsCollectionView.backgroundColor = [UIColor whiteColor];
        _goodsCollectionView.delegate = self;
        _goodsCollectionView.dataSource = self;
        [_goodsCollectionView registerNib:[UINib nibWithNibName:@"GoodsListColCell" bundle:nil] forCellWithReuseIdentifier:@"GoodsListColCellID"];
        [_goodsCollectionView registerNib:[UINib nibWithNibName:@"GoodsListColReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GoodsListColReusableViewID"];
    }
    return _goodsCollectionView;
}

- (void)setPage:(NSInteger)page {
    _page = page;
    [self.paramDic setValue:[NSString stringWithFormat:@"%ld", self.page] forKey:@"page"];
}

- (NSMutableDictionary *)paramDic {
    if (!_paramDic) {
        _paramDic = [NSMutableDictionary dictionary];
    }
    return _paramDic;
}

- (NSMutableArray *)goodsDataArray {
    if (!_goodsDataArray) {
        _goodsDataArray = [NSMutableArray array];
    }
    return _goodsDataArray;
}

@end
